﻿using LogHelper.SQLiteORM;
using LogHelper.SQLiteORM.Attributes;
using System;
using System.Management;
using System.Runtime.Caching;

namespace LogHelper
{
    /// <summary>
    /// 日志内容，包含所有自定义的属性
    /// </summary>
    [Table(Name = "Log")]
    public class LogContent : ILogContent
    {
        private static MemoryCache cacheMac = new MemoryCache("Mac");

        [PrimaryKey(SqliteDataType.Int, IsAutoIncrement = true)]
        public int Id { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Message { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Title { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string UserName { get; set; }
        /// <summary>
        /// mac地址
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Mac { get; set; }
        /// <summary>
        /// 版本号
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Version { get; set; }
        /// <summary>
        /// 程序
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Program { get; set; }
        /// <summary>
        /// 预留信息字段
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string Info { get; set; }
        /// <summary>
        /// 堆栈跟踪串
        /// </summary>
        [Column(SqliteDataType.NVarchar)]
        public string StackTrace
        {
            get
            {
                return Error == null ? "" : Error.StackTrace;
            }
        }
        /// <summary>
        /// 日志时间
        /// </summary>
        [Column(SqliteDataType.DateTime)]
        public DateTime Date { get; set; }
        /// <summary>
        /// 异常
        /// </summary>
        [Ignore]
        public Exception Error { get; set; }

        /// <summary>
        /// 构造函数，可获取本机Mac地址
        /// </summary>
        /// <param name="getLocalMac">是否获取本机Mac地址，为true时，Mac属性值为本机Mac地址</param>
        public LogContent(bool getLocalMac=false)
        {
            Message = "";
            Title = "";
            UserName = "";
            Version = "";
            Program = "";
            Info = "";
            Date = DateTime.Now;
            if (getLocalMac)
            {
                if (cacheMac["Mac"] == null)
                {
                    string strMac = string.Empty;
                    try
                    {
                        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                        ManagementObjectCollection moc = mc.GetInstances();
                        foreach (ManagementObject mo in moc)
                        {
                            if ((bool)mo["IPEnabled"] == true)
                            {
                                strMac = mo["MacAddress"].ToString();
                            }
                        }
                        moc = null;
                        mc = null;
                    }
                    catch
                    {
                        strMac = "unknown";
                    }

                    cacheMac["Mac"] = strMac;
                }
                Mac = (string)cacheMac["Mac"];
            }
        }
    }
}
